/**
 * 自动填充
 */
(function(){
var SearchWordAC = function(inputID, url){ return new SearchWordAC.prototype.init(inputID, url);}

SearchWordAC.prototype.init = function(inputID, url, autoGet)
{	
	this.url = url || '';	
	this.liNum = -1;
	this.input = $(inputID);
	if(!this.input[0])
		return false;	
	
	var l = document.createElement("ul");
	this.l = l;
	var o=this;
	
	// list
	l.className = 'searchWord-list';
	l.onclick = function(e){ if (e) e.stopPropagation(); else window.event.cancelBubble = true;};
	$(l).css({zIndex:1001,position:'absolute'}); 
	$('body').append(l);
	$('body').bind( 'click', function(){ l.style.display = 'none';});
	
	
	// press
	var oI = this.input;
	oI.bind('keydown', function(){o.keydown();});
	oI.bind('keyup', function(){o.keyup();});
	
	//autocomplete
	oI.one('change', function(){ o.input.attr('autocomplete', 'off'); });

    return this;
}
SearchWordAC.prototype.init.prototype = SearchWordAC.prototype;

/**
 * 按下
 * @returns {Boolean}
 */
SearchWordAC.prototype.keydown = function()
{
	var e = window.event || arguments.callee.caller.arguments[0];		
	if (e.keyCode == 27)//Esc
	  {  
		this.l.style.display = 'none';
	  }
	else if (e.keyCode == 13 )//Enter
	 {		
		if(this.l.style.display=='none' || this.l.innerHTML=='' )
			return true;
		
		$(this.l.childNodes.item(this.liNum)).click();
		
		if ( e.preventDefault ) 
        	e.preventDefault();
		else
			e.returnValue = false;
	 }
}
SearchWordAC.prototype.keyup = function()
{
	var e = window.event || arguments.callee.caller.arguments[0];
	if (e.keyCode == 27 || e.keyCode == 13)
		return;
	else if(e.keyCode == 38 || e.keyCode == 40) // up down
	 {  
		if(this.l.style.display=='none' || this.l.innerHTML=='' )
			  return;
		
		if(e.keyCode == 38 && this.liNum>0)
			this.liNum--;	
		else if(e.keyCode == 40 && this.liNum < this.l.childNodes.length-1)
			this.liNum++;
			
		$(this.l).children('li').removeClass('hover').eq(this.liNum).addClass('hover');
	 }
	else if(this.input[0].value)
	 {
		 clearTimeout(this.___isget);
		 var o = this;
		 this.___isget = setTimeout(function(){o.get()}, 600);		
	 }	
	else
		this.l.style.display = 'none';
		
}
SearchWordAC.prototype.get = function(keyword, call)
{
	if (!this.url)return;
	
	//修正 hide 时不能定位	
	var po = this.input.offset();
	var o = this;
	if (typeof(keyword)=='undefined' )
		keyword = this.input[0].value;
	
	this.input.addClass('searchWord-loading');
	$.get(this.url + encodeURIComponent(keyword), function(r)
	{
		o.input.removeClass('searchWord-loading');
		if(!checAjaxRe(r))
			return;
			
		o.liNum = -1;
		o.l.innerHTML = '';
		o.l.style.display = 'none';
		if(r.res.length>0)
		 {	
			for(i in r.res)
			 {
				var data = r.res[i];
				var li = document.createElement('li');
				o.l.appendChild(li);
				
				var lio = $(li).bind('click',{a:data},function(event){o.selectFun(this, event.data.a)});
				li.onmouseover = function (){
					$(o.l).children('li').removeClass('hover');
					o.liNum = $(this).addClass('hover').index();		
				 }
				
				if (o.setLiFun)
					o.setLiFun(li, data);
				else
					lio.html(data.id ? data.id+'.'+data.name : data.name);		 
			  }
			var ol = $(o.l).css({left:po.left, top:po.top+o.input.innerHeight(), width:o.input.outerWidth()-2});
			ol.slideDown('normal',function(){ if( o.l.scrollWidth > ol.width() ){ol.animate({width:o.l.scrollWidth+20});}});		
		  }
		  
		if(call)
			call(o);
	});
}
SearchWordAC.prototype.selectFun = function(li, data){
		this.input[0].value = data.id ? data.id :$(li).text();
		this.input.focus();
		$(this.l).slideUp();
}
window.SearchWordAC = SearchWordAC;
})();

